package cn.test.rabbitmq.spring;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

/**
 * @author hanchao
 * @date 2017/7/3 18:33
 */
public class MessageConsumer1 implements ChannelAwareMessageListener {


    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("normal message>>> "+message);
        //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        if(message.getMessageProperties().isRedelivered()){
            Object consumeCount = message.getMessageProperties().getHeaders().get("_consumeCount");
            int i = 2;
            if(consumeCount != null){
                i = (int) consumeCount;
            }
            if(consumeCount == null){
                message.getMessageProperties().setHeader("_consumeCount",2);
            }else{
                message.getMessageProperties().setHeader("_consumeCount",i+1);
            }
        }
        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
    }
}
